import 'dart:convert';

import 'package:audioplayers/audio_cache.dart';
import 'package:bruno/bruno.dart';
import 'package:demo/common/util.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:getwidget/getwidget.dart';
import '../service/resouceService.dart';

class keySoundPage extends StatefulWidget {
  @override
  _keySoundPage createState() => _keySoundPage();
}

class _keySoundPage extends State<keySoundPage> {
  List soundList = [];
  var _futureBuilderFuture;

  // 按鍵音播放
  AudioCache audioCache = AudioCache();
  void playSound(soundSrc) {
    audioCache.play(soundSrc);
  }

  // 獲取按鍵音數據
  Object getThemeData() async {
    var params = {"pageNum": 1, "pageSize": 10, "type": 2};
    var response = await resouceService.list(params);
    setState(() {
      this.soundList = response.data;
    });
    print(response);
    return null;
  }

  @override
  void initState() {
    _futureBuilderFuture = getThemeData();
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        appBar: BrnAppBar(
          title: Text(
            '按鍵音列表',
            style: TextStyle(color: Colors.black),
          ),
          backgroundColor: Color.fromARGB(255, 237, 236, 236),
          // elevation: 0,
        ),
        body: FutureBuilder(
            future: _futureBuilderFuture,
            builder: (BuildContext context, AsyncSnapshot snapshot) {
              print(snapshot.data);
              return RefreshIndicator(
                  onRefresh: () async {
                    getThemeData();
                  },
                  child: Container(
                      child: Column(children: [
                    Container(
                      height: MediaQuery.of(context).size.height * 0.8,
                      padding: EdgeInsets.only(top: 10),
                      child: ListView.builder(
                          itemCount: this.soundList.length,
                          itemBuilder: (context, index) {
                            return GFListTile(
                              title: Text(
                                  this.soundList[index]['title'].toString()),
                              subTitleText:
                                  this.soundList[index]['desc'].toString(),
                              avatar: GFAvatar(
                                backgroundColor:
                                    Color.fromARGB(255, 2, 114, 170),
                              ),
                              icon: GFButton(
                                text: '試聽',
                                onPressed: () {
                                  this.playSound(
                                      this.soundList[index]['src'].toString());
                                },
                              ),
                              color: Color.fromARGB(95, 188, 188, 188),
                            );
                          }),
                    ),
                  ])));
            }));
  }
}
